package com.adk.backend.controller;

import com.adk.backend.common.PageResult;
import com.adk.backend.common.Result;
import com.adk.backend.entity.LxClientList;
import com.adk.backend.service.ClientListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 贸易客户列表控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/client-list")
public class ClientListController {
    
    @Autowired
    private ClientListService clientListService;
    
    /**
     * 获取客户列表
     */
    @GetMapping("/list")
    public Result<PageResult<Map<String, Object>>> getClientList(
            @RequestParam(required = false) Integer yw,
            @RequestParam(required = false) String ccName,
            @RequestParam(required = false) String ccShort,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "20") Integer pageSize,
            HttpServletRequest request) {
        
        Map<String, Object> params = new HashMap<>();
        params.put("yw", yw);
        params.put("ccName", ccName);
        params.put("ccShort", ccShort);
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        
        // 处理用户权限：用户组4或6只能查看自己的客户
        Integer userGroup = (Integer) request.getAttribute("userGroup");
        Long userId = (Long) request.getAttribute("userId");
        if ((userGroup != null && (userGroup == 4 || userGroup == 6)) && userId != null) {
            params.put("userId", userId);
        }
        
        PageResult<Map<String, Object>> result = clientListService.getClientList(params);
        return Result.success(result);
    }
    
    /**
     * 获取客户详情
     */
    @GetMapping("/detail")
    public Result<Map<String, Object>> getClientDetail(@RequestParam Integer id) {
        Map<String, Object> client = clientListService.getClientById(id);
        if (client == null) {
            return Result.error("客户不存在");
        }
        return Result.success(client);
    }
    
    /**
     * 创建客户
     */
    @PostMapping("/create")
    public Result<LxClientList> createClient(@RequestBody LxClientList clientList, HttpServletRequest request) {
        // 检查客户名称或简称是否已存在
        if (clientListService.checkClientExists(clientList.getCcName(), clientList.getCcShort(), null)) {
            return Result.error("公司名称或简称已存在，无法添加");
        }
        
        Long userId = (Long) request.getAttribute("userId");
        LxClientList created = clientListService.createClient(clientList, userId);
        return Result.success("创建成功", created);
    }
    
    /**
     * 更新客户
     */
    @PostMapping("/update")
    public Result<Boolean> updateClient(@RequestBody LxClientList clientList, HttpServletRequest request) {
        // 检查客户名称或简称是否已存在
        if (clientListService.checkClientExists(clientList.getCcName(), clientList.getCcShort(), clientList.getId())) {
            return Result.error("公司名称或简称已存在，无法更新");
        }
        
        Long userId = (Long) request.getAttribute("userId");
        boolean success = clientListService.updateClient(clientList, userId);
        if (success) {
            return Result.success("更新成功", true);
        } else {
            return Result.error("更新失败");
        }
    }
    
    /**
     * 删除客户
     */
    @PostMapping("/delete")
    public Result<Boolean> deleteClient(@RequestParam Integer id) {
        boolean success = clientListService.deleteClient(id);
        if (success) {
            return Result.success("删除成功", true);
        } else {
            return Result.error("删除失败");
        }
    }
}

